/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * modificarUsuario.java
 *
 * Created on 26-abr-2011, 2:11:13
 */

package interfaces;

import Controlador.Controlador;
import GestorClinico.Direccion;
import GestorUsuarios.GeneroUsuario;
import GestorUsuarios.Usuario;
import clinica.ClinicaView;
import clinica.Interfaces;
import clinica.Interfaz;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jdesktop.application.Action;

/**
 *
 * @author Victor
 */
public class modificarUsuario extends javax.swing.JPanel {
        
    private ClinicaView app;
    private Usuario usuario;

    /** Creates new form modificarUsuario */
    public modificarUsuario(ClinicaView a) {
        initComponents();
        app = a;
        direcciones.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        direcciones.setRowHeight(32);
    }

    public void rellenaDatos(Usuario u) throws SQLException, IOException, ClassNotFoundException {
        usuario = u;
        nomb.setText(u.getNombre());
        apell.setText(u.getApellidos());
        correoele.setText(u.getCorreo());
        DNIpass.setText(u.getDniPasaporte());
        telefono.setText(""+u.getTelefonoMovil());
        fecha.setDate(u.getFechaNacimiento());
        if(u.getGenero()==GeneroUsuario.MASCULINO)
            hombre.setSelected(true);
        else mujer.setSelected(true);

        while( ((DefaultTableModel)direcciones.getModel()).getRowCount() > 0 )
            ((DefaultTableModel)direcciones.getModel()).removeRow(0);

        Direccion [] dires = u.getDirecciones();
        if(dires!=null)
        for(Direccion d:dires) {
            String [] valores = {d.getDireccion(),d.getLocalidad(),""+d.getCP(),d.getPais(),""+d.getTelefono()};
            ((DefaultTableModel)direcciones.getModel()).addRow(valores);
        }
    }

     public void addDireccion(String direccion, String localidad, String CP, String pais, String telefono) {
        String [] valores = {direccion,localidad,CP,pais,telefono};
            ((DefaultTableModel)direcciones.getModel()).addRow(valores);
    }

    public void modDireccion(int dir, String direccion, String localidad, String CP, String pais, String telefono) {
            ((DefaultTableModel)direcciones.getModel()).setValueAt(direccion, dir, 0);
            ((DefaultTableModel)direcciones.getModel()).setValueAt(localidad, dir, 1);
            ((DefaultTableModel)direcciones.getModel()).setValueAt(CP, dir, 2);
            ((DefaultTableModel)direcciones.getModel()).setValueAt(pais, dir, 3);
            ((DefaultTableModel)direcciones.getModel()).setValueAt(telefono, dir, 4);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        nomb = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        apell = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        DNIpass = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        correoele = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        telefono = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        fecha = new com.toedter.calendar.JDateChooser();
        jLabel16 = new javax.swing.JLabel();
        hombre = new javax.swing.JRadioButton();
        mujer = new javax.swing.JRadioButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        direcciones = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel4.setName("jPanel4"); // NOI18N

        jLabel9.setFont(Interfaz.setFontSize());
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(modificarUsuario.class);
        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        nomb.setFont(Interfaz.setFontSize());
        nomb.setName("nomb"); // NOI18N

        jLabel10.setFont(Interfaz.setFontSize());
        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        apell.setFont(Interfaz.setFontSize());
        apell.setName("apell"); // NOI18N

        jLabel11.setFont(Interfaz.setFontSize());
        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        DNIpass.setFont(Interfaz.setFontSize());
        DNIpass.setName("DNIpass"); // NOI18N

        jLabel12.setFont(Interfaz.setFontSize());
        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        correoele.setFont(Interfaz.setFontSize());
        correoele.setName("correoele"); // NOI18N

        jLabel13.setFont(Interfaz.setFontSize());
        jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
        jLabel13.setName("jLabel13"); // NOI18N

        telefono.setFont(Interfaz.setFontSize());
        telefono.setName("telefono"); // NOI18N

        jLabel14.setFont(Interfaz.setFontSize());
        jLabel14.setText(resourceMap.getString("jLabel14.text")); // NOI18N
        jLabel14.setName("jLabel14"); // NOI18N

        fecha.setFont(Interfaz.setFontSize());
        fecha.setName("fecha"); // NOI18N

        jLabel16.setFont(Interfaz.setFontSize());
        jLabel16.setText(resourceMap.getString("jLabel16.text")); // NOI18N
        jLabel16.setName("jLabel16"); // NOI18N

        buttonGroup1.add(hombre);
        hombre.setFont(Interfaz.setFontSize());
        hombre.setText(resourceMap.getString("hombre.text")); // NOI18N
        hombre.setName("hombre"); // NOI18N

        buttonGroup1.add(mujer);
        mujer.setFont(Interfaz.setFontSize());
        mujer.setText(resourceMap.getString("mujer.text")); // NOI18N
        mujer.setName("mujer"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 728, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );

        jPanel2.setName("jPanel2"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 728, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 42, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel16, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(hombre, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mujer, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel14, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(12, 12, 12)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nomb, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
                            .addComponent(apell, javax.swing.GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
                            .addComponent(correoele, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
                            .addComponent(DNIpass, javax.swing.GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
                            .addComponent(telefono, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
                            .addComponent(fecha, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nomb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(apell, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DNIpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(correoele, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(telefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hombre)
                    .addComponent(mujer)
                    .addComponent(jLabel16))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Datos usuario", jPanel4);

        jPanel5.setName("jPanel5"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        direcciones.setFont(Interfaz.setFontSize());
        direcciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Direccion", "Localidad", "Cod.Postal", "Pais", "Telefono"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        direcciones.setName("direcciones"); // NOI18N
        jScrollPane1.setViewportView(direcciones);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(modificarUsuario.class, this);
        jButton2.setAction(actionMap.get("modDir")); // NOI18N
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setAction(actionMap.get("delDir")); // NOI18N
        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        jButton1.setAction(actionMap.get("addDir")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 664, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButton1)
                        .addComponent(jButton2))
                    .addComponent(jButton3))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Direcciones usuario", jPanel5);

        jButton4.setAction(actionMap.get("aceptar")); // NOI18N
        jButton4.setIcon(resourceMap.getIcon("jButton4.icon")); // NOI18N
        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N

        jButton5.setAction(actionMap.get("volver")); // NOI18N
        jButton5.setIcon(resourceMap.getIcon("jButton5.icon")); // NOI18N
        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 760, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton4)
                    .addComponent(jButton5))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    public Direccion [] getDirecciones() {
        int rowCount = direcciones.getRowCount();

        if(rowCount==0)
            return null;

        Direccion [] dires = new Direccion[rowCount];
        for(int i=0;i<rowCount;i++) {
            String direccion = (String) ((DefaultTableModel)direcciones.getModel()).getValueAt( i, 0);
            String localidad = (String) ((DefaultTableModel)direcciones.getModel()).getValueAt( i, 1);
            int CP = Integer.parseInt((String)((DefaultTableModel)direcciones.getModel()).getValueAt( i, 2));
            String pais = (String) ((DefaultTableModel)direcciones.getModel()).getValueAt( i, 3);
            int telefono = Integer.parseInt((String)((DefaultTableModel)direcciones.getModel()).getValueAt( i, 4));

            dires[i] = new Direccion(direccion, localidad, CP, pais, telefono);
        }

        return dires;
    }

    public void modUsuario() throws NoSuchAlgorithmException, IOException, ClassNotFoundException {
        GeneroUsuario genero;
            if(hombre.isSelected())
                genero = GeneroUsuario.MASCULINO;
            else genero = GeneroUsuario.FEMENINO;


            String res = Controlador.modificarUsuario(usuario.getIdUsuario(),DNIpass.getText(), nomb.getText(), apell.getText(),
                    genero, correoele.getText(), getDirecciones(), Integer.parseInt(telefono.getText()),
                    fecha.getDate(),null, null, null);
            if(res==null)
                app.abrirMensajeExito("Usuario Modificado correctamente.", Interfaces.BUSCAR_USUARIOS);
            else app.abrirMensajeError(res, Interfaces.BUSCAR_USUARIOS2);
    }

    @Action
    public void modDir() {
        int row = direcciones.getSelectedRow();
        if(row>=0 && row<direcciones.getRowCount()) {
            TableModel m = direcciones.getModel();

            app.getVenanaDirecciones().rellenaEntradas((String)m.getValueAt(row, 0),
            (String)m.getValueAt(row, 1), (String)m.getValueAt(row, 2), (String)m.getValueAt(row, 3),
                    (String)m.getValueAt(row, 4),row);
            app.getVenanaDirecciones().setPadre(null,this);
            app.getVenanaDirecciones().setVisible(true);
        }
    }

    @Action
    public void delDir() {
        int row = direcciones.getSelectedRow();
        if(row>=0 && row<direcciones.getRowCount())
            ((DefaultTableModel)direcciones.getModel()).removeRow(row);
    }

    @Action
    public void addDir() {
        app.getVenanaDirecciones().limpiaEntradas();
        app.getVenanaDirecciones().setPadre(null,this);
        app.getVenanaDirecciones().setVisible(true);
    }

    @Action
    public void aceptar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException {
                // Comprobacion de errores
        boolean hay_errores = false;
        app.limpiaErrores();

        String expresion_numero = "^\\d{1,9}$";
	Pattern pattern = Pattern.compile(expresion_numero);
	Matcher matcher = pattern.matcher(telefono.getText());

        if(nomb.getText().length()< 4) {
            app.addError("El nombre debe tener más de 4 caracteres.");
            hay_errores = true;
        }
        if(nomb.getText().length() > 64) {
            app.addError("El nombre debe tener menos de 64 caracteres.");
            hay_errores = true;
        }

        if(apell.getText().length()< 4) {
            app.addError("El apellido debe tener más de 4 caracteres.");
            hay_errores = true;
        }
        if(apell.getText().length() > 128) {
            app.addError("El apellido debe tener menos de 128 caracteres.");
            hay_errores = true;
        }

        if(DNIpass.getText().length()< 4) {
            app.addError("El DNI o pasaporte debe tener más de 4 caracteres.");
            hay_errores = true;
        }
        if(DNIpass.getText().length() > 16) {
            app.addError("El DNI o pasaporte debe tener menos de 16 caracteres.");
            hay_errores = true;
        }

        if(correoele.getText().length() == 0) {
            app.addError("Introduzca el correo.");
            hay_errores = true;
        }
        if(correoele.getText().length() > 256) {
            app.addError("El correo debe tener menos de 256 caracteres.");
            hay_errores = true;
        }

        if(fecha.getDate()==null) {
            app.addError("Debe seleccionar una fecha de nacimiento.");
            hay_errores = true;
        }

        Calendar hoy = Calendar.getInstance();
        if(fecha.getDate().after(hoy.getTime())) {
            app.addError("La fecha es mayor que el día actual.");
            hay_errores = true;
        }

        if(!matcher.matches()) {
            app.addError("El movil es incorrecto, debe contener 9 dígitos.");
            hay_errores = true;
        }

        if(hay_errores==true)
            app.muestraListaErrores();
        else this.modUsuario();
    }

    @Action
    public void volver() {
        app.volver(Interfaces.BUSCAR_USUARIOS2);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField DNIpass;
    private javax.swing.JTextField apell;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField correoele;
    private javax.swing.JTable direcciones;
    private com.toedter.calendar.JDateChooser fecha;
    private javax.swing.JRadioButton hombre;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JRadioButton mujer;
    private javax.swing.JTextField nomb;
    private javax.swing.JTextField telefono;
    // End of variables declaration//GEN-END:variables

}
